package action.business.order;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import service.BusinessOrderService;
import service.PointService;
import vo.OrderListVO;
import vo.OrderVO;
import vo.PointVO;

public class AcceptOrderAction{

	private String order_id;
	private String member_id;
	private String pid;
	private ArrayList<OrderVO> orderVOList;
	private ArrayList<OrderListVO> list;
	private String command;
	private int totalSum;
	
	private BusinessOrderService businessOrderService;
	private PointService pointService;
	
	public String getOrder_id() {
		return order_id;
	}
	public void setOrder_id(String orderId) {
		order_id = orderId;
	}
	public String getMember_id() {
		return member_id;
	}
	public void setMember_id(String memberId) {
		member_id = memberId;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public BusinessOrderService getBusinessOrderService() {
		return businessOrderService;
	}
	public void setBusinessOrderService(BusinessOrderService businessOrderService) {
		this.businessOrderService = businessOrderService;
	}
	public String getCommand() {
		return command;
	}
	public void setCommand(String command) {
		this.command = command;
	}
	public ArrayList<OrderVO> getOrderVOList() {
		return orderVOList;
	}
	public void setOrderVOList(ArrayList<OrderVO> orderVOList) {
		this.orderVOList = orderVOList;
	}
	public int getTotalSum() {
		return totalSum;
	}
	public void setTotalSum(int totalSum) {
		this.totalSum = totalSum;
	}
	public PointService getPointService() {
		return pointService;
	}
	public void setPointService(PointService pointService) {
		this.pointService = pointService;
	}
	
	public String execute(){
		
		try {
			if(command.equals("accept_order")){
				businessOrderService.updateAcceptOrder(order_id, member_id);
			}else if(command.equals("translate")){
				businessOrderService.updateTranslateOrder(order_id, member_id);
			}else if(command.equals("complete")){
				businessOrderService.updateCompleteOrder(order_id, member_id);
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("order_id", order_id);
				map.put("member_id", member_id);
				list = businessOrderService.selectOrderListById(map);
				for(OrderListVO l : list){
					PointVO pvo = new PointVO(l.getOrder_id(), l.getPid(), l.getPrice(), l.getMember_id());
					pointService.insertSellPoint(pvo);
				}
			}
			
			ArrayList<String> orderIds = businessOrderService.selectOrderId(member_id);
			orderVOList = businessOrderService.selectOrderVO(orderIds, member_id);
			for(OrderVO ovo : orderVOList){
				for(OrderListVO list : ovo.getList()){
					totalSum = totalSum + list.getTotalPrice();
				}
				ovo.setSumprice(totalSum);
				totalSum = 0;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	
}
